package password.secret;

import java.io.ByteArrayOutputStream;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class PasswordActivity extends Activity {
    /** Called when the activity is first created. */
	private Button btnSpeak;
	private Button decrypt;
	private EditText txtText;
	private EditText txtTextE;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnSpeak = (Button) findViewById(R.id.btnSpeak);
        decrypt= (Button) findViewById(R.id.decrypt);
		txtText = (EditText) findViewById(R.id.txtText);
		txtTextE = (EditText) findViewById(R.id.txtTextE);
		btnSpeak.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				try {
					txtText.setText(SimpleCrypto.encrypt("dhiraj", txtTextE.getText().toString()), TextView.BufferType.EDITABLE);
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		decrypt.setOnClickListener(new View.OnClickListener(){

			public void onClick(View arg0) {
				try {
					txtText.setText(SimpleCrypto.decrypt("dhiraj", txtText.getText().toString()), TextView.BufferType.EDITABLE);
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		
    }
      public void encrypt ()
      {
    	
        try{
        //	AssetManager am = activity.getAssets();
     //       InputStream is = am.open("myfile.enc");

   //     	Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.shit);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
      //      bm.compress(Bitmap.CompressFormat.PNG, 100, baos); // bm is the bitmap object   
            byte[] b = baos.toByteArray();  

            
            byte[] keyStart = "MARTIN_123_MARTIN_123".getBytes();
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
            sr.setSeed(keyStart);
            kgen.init(128, sr); // 192 and 256 bits may not be available
            SecretKey skey = kgen.generateKey();
            byte[] key = skey.getEncoded();    

            // encrypt
            byte[] encryptedData = encrypt(key,b);
            // decrypt
            long start = System.currentTimeMillis()/1000L;
            byte[] decryptedData = decrypt(key,encryptedData);
            long end = System.currentTimeMillis()/1000L;

        }
        catch(Exception e)
        {
        	
        }
      }
      private byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
          SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
          byte[] encrypted = cipher.doFinal(clear);
          return encrypted;
      }

      private byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
          SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.DECRYPT_MODE, skeySpec);
                  byte[] decrypted = cipher.doFinal(encrypted);
      return decrypted;
      }
  }
 